# load charging data from XLS file
# rawdata <- read_excel("ladesaeulen.xlsx", sheet="Sachdaten")
view(rawdata)
charging_stations <- st_as_sf(rawdata, coords=c("x_wert", "y_wert"), crs=25833)
# take a look using tmap
tmap_mode("view") # tmap_mode("plot")
## tmap mode set to interactive viewing
tm_shape(charging_stations) + tm_dots(id="adresse")
# Now load the Berlin neighborhoods
# bezirke <- st_read("shp-bezirke/bezirke_berlin.shp")
tm_shape(bezirke) + tm_polygons(col="SCHLUESSEL") +
tm_shape(charging_stations) + tm_dots()
bezirke <- st_transform(bezirke, crs=25833)
mitte <- bezirke %>% filter(SCHLUESSEL == "010113")
# st_intersects(mitte, charging_stations, sparse=FALSE)
# use ugly dplyr
mitte_charging_stations <- charging_stations %>%
filter(st_intersects(., mitte, sparse=FALSE))
tm_shape(mitte_charging_stations) + tm_dots()
# or use ugly base R
mitte_charging_stations <- charging_stations[mitte,]
tm_shape(mitte_charging_stations) + tm_dots()
# all together!
tm_shape(bezirke) +
tm_polygons() +
tm_shape(mitte_charging_stations) +
tm_dots()
# Summary statistics: how many e-stations are in each district?
joined <- st_join(bezirke, charging_stations)
view(joined)
charger_count <- joined %>%
group_by(SCHLUESSEL) %>%
summarize(count=n())
## `summarise()` ungrouping output (override with `.groups` argument)
tm_shape(charger_count) +
tm_polygons(col="count")
setwd("~/Workspace/R/Data Science for agent-based transport simulations/week10/week10")
# Exercise 10-1
# bezirke <- st_read("shp-bezirke/bezirke_berlin.shp")
bezirke <- st_transform(bezirke, crs=25833)
accident_rawdata <- read.csv2("Unfaelle.csv")
accident_shape_points <- accident_rawdata %>%
st_as_sf(coords = c("XGCSWGS84", "YGCSWGS84"), crs = 4326) %>%
st_transform(crs = 25833) %>%
mutate(IstRad = as.logical(IstRad))
tm_shape(bezirke) +
tm_polygons(alpha = 0.7) +
tm_shape(accident_shape_points) +
tm_dots(col = "IstRad", size = 0.01)
# Exercise 10-2
joined <- st_join(bezirke, accident_shape_points)
accident_count <- joined %>%
group_by(SCHLUESSEL) %>%
summarise(count=n())
## `summarise()` ungrouping output (override with `.groups` argument)
tm_shape(accident_count) +
# tm_borders() +
tm_polygons() +
tm_dots(size="count", col="count")
## Legend for symbol sizes not available in view mode.
setwd("~/Workspace/R/Data Science for agent-based transport simulations/week10/week10")
links = read_csv("Links_with_loads_geo2.csv",
locale=locale(encoding="latin1"))
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## .default = col_double(),
## From_Station = col_character(),
## To_Station = col_character(),
## Line = col_character(),
## Line_Direction = col_character(),
## Platform_Direction = col_character(),
## NAPTAN_From = col_character(),
## NAPTAN_To = col_character(),
## Direction = col_character(),
## Zone.1 = col_number(),
## Postcode.1 = col_character(),
## Zone.2 = col_number(),
## Postcode.2 = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
# glimpse(links)
links <- mutate(links,
wkt=sprintf(
"LINESTRING(%s %s, %s %s)",
Longitude.1,Latitude.1,Longitude.2,Latitude.2))
df <- st_as_sf(links, wkt="wkt", dim="XY", crs=4326)
tm_shape(df) + tm_lines()
# tm_shape(df) + tm_lines(col="Line")
# tm_shape(df) + tm_lines(col="Line", lwd="X0800.0815")
tm_shape(df) + tm_lines(col="Line", lwd="X0800.0815", scale=10)
## Legend for line widths not available in view mode.
# london <- st_point(c(-0.08662, 51.50435))
# london
# london_sfc <-st_sfc(london, crs=27700)
# london_sfc
# df <- filter(df, st_is_within_distance(df, london, dist = 10000, sparse=FALSE))
# ?filter